package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotSchoolClassesRelation;
import com.ruoyi.wisdom.domain.swap.SwapSchoolClassesRelation;
import com.ruoyi.wisdom.service.IIotSchoolClassesRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolClassesRelationEvent extends IotEvent{

    @Autowired
    private IIotSchoolClassesRelationService iotSchoolClassesRelationService;

    /**
     * 处理
     * @param swapSchoolClassesRelation
     */
    public void handle(SwapSchoolClassesRelation swapSchoolClassesRelation) {
        if (swapSchoolClassesRelation.getOperate() == null) {
            save(swapSchoolClassesRelation);
        } else {
            switch (swapSchoolClassesRelation.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapSchoolClassesRelation);
                    break;
                default:
                    save(swapSchoolClassesRelation);
            }
        }
    }

    /**
     * 添加|编辑
     * @param swapSchoolClassesRelation
     */
    public void save(SwapSchoolClassesRelation swapSchoolClassesRelation)
    {
        IotSchoolClassesRelation result = iotSchoolClassesRelationService.selectIotSchoolClassesRelationById(swapSchoolClassesRelation.getId());
        // 实体装换
        IotSchoolClassesRelation iotSchoolClassesRelation = getModelMapper().map(swapSchoolClassesRelation, IotSchoolClassesRelation.class);
        if (result != null) {
            iotSchoolClassesRelation.setId(swapSchoolClassesRelation.getId());
            iotSchoolClassesRelationService.updateIotSchoolClassesRelation(iotSchoolClassesRelation);
        } else {
            iotSchoolClassesRelationService.insertIotSchoolClassesRelation(iotSchoolClassesRelation);
        }
    }

    /**
     * 删除
     * @param swapSchoolClassesRelation
     */
    public void delete(SwapSchoolClassesRelation swapSchoolClassesRelation){
        iotSchoolClassesRelationService.deleteIotSchoolClassesRelationById(swapSchoolClassesRelation.getId());
    }
}
